<template>
  <div class="p-15">
    <el-card style="margin: 10px 0">
        <el-table style="margin: 10px 0" :data="tableData">
          <el-table-column align="center" prop="item.name" label="人物">
            <template slot-scope="scope">
              <span @click="$router.push('/front/itemDetail?id=' + scope.row.item.id)">{{scope.row.item.name}}</span>
            </template>
          </el-table-column>
      <el-table-column align="center" prop="user.name" label="用户"></el-table-column>
      <el-table-column align="center" prop="time" label="添加时间"></el-table-column>
          <el-table-column align="center" label="图片"><template v-slot="scope"><el-image style="width: 50px; height: 50px" :src="scope.row.item.img" :preview-src-list="[scope.row.item.img]"></el-image></template></el-table-column>

          <el-table-column label="操作" width="180" align="center">
            <template slot-scope="scope">
              <el-button plain type="danger" size="mini" @click=del(scope.row.id)>删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <div style="margin: 10px 0">
          <el-pagination
              style="padding: 0"
              background
              layout="total, prev, pager, next"
              @current-change="handleCurrentChange"
              :current-page="pageNum"
              :page-size="pageSize"
              :total="total">
          </el-pagination>
        </div>
    </el-card>


    <el-dialog title="信息" :visible.sync="dialogFormVisible" width="30%" :close-on-click-modal="false">
      <el-form label-width="100px" style="padding-right: 40px" :model="form" :rules="rules" ref="ruleForm">
        <el-form-item prop="itemId" label="人物">
          <el-input v-model="form.itemId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="userId" label="用户">
          <el-input v-model="form.userId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="time" label="添加时间">
          <el-date-picker style='width:100%' v-model="form.time" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Collect",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: '10',
      name: "",
      form: {},
      dialogFormVisible: false,
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      rules: {
        name: [
          { required: true, message: '请输入必填项', trigger: 'blur'}
        ],
      }
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.$request.get("/collect/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: 10,
          name: this.name,
        }
      }).then(res => {
        this.tableData = res.data?.records
        this.total = res.data?.total
      })
    },
    save() {
        this.$refs['ruleForm'].validate((valid) => {
          if (valid) {
           this.$request({
             method: this.form.id ? 'PUT' : 'POST',
             url: "/collect",
             data: this.form
           }).then(res => {
              if (res.code === '200') {
                this.$message.success("操作成功")
                this.dialogFormVisible = false
                this.load()
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
      this.$nextTick(() => {
        if(this.$refs.img) {
           this.$refs.img.clearFiles();
         }
         if(this.$refs.file) {
          this.$refs.file.clearFiles();
         }
      })
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogFormVisible = true
       this.$nextTick(() => {
         if(this.$refs.img) {
           this.$refs.img.clearFiles();
         }
         if(this.$refs.file) {
          this.$refs.file.clearFiles();
         }
       })
    },
    del(id) {
      this.$confirm('您确认批量删除这些数据吗？', '确认删除', {type: "warning"}).then(response => {
       this.$request.delete("/collect/" + id).then(res => {
          if (res.code === '200') {
            this.$message.success("操作成功")
            this.load()
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {})
    },
    reset() {
      this.name = ""
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    handleFileUploadSuccess(res) {
      this.form.file = res.data
    },
    handleImgUploadSuccess(res) {
      this.form.img = res.data
    }
  }
}
</script>


<style scope>

</style>
